package com.leyou.item.controller;

import com.leyou.item.pojo.Category;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 根据parentId查询类目
     *
     * @param pid
     * @return List<Category>
     */
    @GetMapping("list")
    public ResponseEntity<List<Category>> queryCategoryListByPid(@RequestParam("pid") Long pid){
        try {

            if(pid==null||pid.longValue()<0){
                // 参数错误,响应400
                //return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
                //return ResponseEntity.badRequest().build();
                return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
            }

            //查询数据
            List<Category> categoryList=categoryService.queryCategoryListByPid(pid);

            if(CollectionUtils.isEmpty(categoryList)){
                // 资源服务未找到响应404
                //return new ResponseEntity(HttpStatus.NOT_FOUND);
                return ResponseEntity.notFound().build();
            }

            // 响应200 get-200 put,delete-204 post-201
            //return ResponseEntity.status(HttpStatus.OK).body(categoryList);
            return ResponseEntity.ok(categoryList);
        }catch (Exception e){
            e.printStackTrace();
        }
        // 服务器内部错误,响应500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 保存分类数据
     * @param category
     * @return
     */
    @PostMapping("add")
    public ResponseEntity<Category> saveCategory(Category category) {
        try {
            Category c = categoryService.save(category);
            return new ResponseEntity<Category>(c,HttpStatus.CREATED);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 更新分类数据
     * @param category
     * @return
     */
    @PutMapping("update")
    public ResponseEntity<Void> updateCategory(Category category) {
        try {
            categoryService.update(category);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 删除分类数据
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public ResponseEntity<Void> deleteCategory(@PathVariable("id") Long id) {
        try {
            categoryService.delete(id);
            return new ResponseEntity<>(HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 根据BrandId查询分类数据
     * @param bid
     * @return
     */
    @GetMapping("bid/{bid}")
    public ResponseEntity<List<Category>> queryCategoryListByBrandId(@PathVariable("bid") Long bid){
        try {
            List<Category> categories=categoryService.queryCategoryListByBrandId(bid);
            if(CollectionUtils.isEmpty(categories)){
                return new ResponseEntity<>(HttpStatus.NOT_FOUND);
            }
            return ResponseEntity.ok(categories);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 根据商品分类id查询名称
     * @param ids 要查询的分类id集合
     * @return 多个名称的集合
     */
    @GetMapping("names")
    public ResponseEntity<List<String>> queryNamesByIds(@RequestParam("ids") List<Long> ids){
        try {
            List<String> names = categoryService.queryNamesByIds(ids);
            if(CollectionUtils.isEmpty(names)){
                return new ResponseEntity<>(HttpStatus.NOT_FOUND);
            }
            return ResponseEntity.ok(names);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("/all/level")
    public ResponseEntity<List<Category>> queryAllByCid3(@RequestParam("id") Long id){
       List<Category> list= categoryService.queryAllByCid3(id);

        if (CollectionUtils.isEmpty(list)) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(list);
    }

}
